/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package per.wsj.andratingbar.ui;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.Context;
import per.wsj.andratingbar.ResourceTable;
import per.wsj.library.AndRatingBar;

/**
 * StyleFragment
 *
 * @since 2021-04-13
 */
public class StyleFragment extends Fraction {
    private static final float SCORE = 2.5f;
    private AndRatingBar ratingBar3;
    private AndRatingBar rb1;
    private AndRatingBar rb2;
    private AndRatingBar rb3;
    private AndRatingBar rb4;
    private AndRatingBar rb5;
    private AndRatingBar rb6;
    private Text tvRate;

    private Context mContext;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public StyleFragment(Context context) {
        this.mContext = context;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = LayoutScatter.getInstance(this.mContext).parse(ResourceTable.Layout_fragment_style,
                container, false);
        ratingBar3 = (AndRatingBar) component.findComponentById(ResourceTable.Id_ratingBar3);
        rb1 = (AndRatingBar) component.findComponentById(ResourceTable.Id_rb1);
        rb2 = (AndRatingBar) component.findComponentById(ResourceTable.Id_rb2);
        rb3 = (AndRatingBar) component.findComponentById(ResourceTable.Id_rb3);
        rb4 = (AndRatingBar) component.findComponentById(ResourceTable.Id_rb4);
        rb5 = (AndRatingBar) component.findComponentById(ResourceTable.Id_rb5);
        rb6 = (AndRatingBar) component.findComponentById(ResourceTable.Id_rb6);
        tvRate = (Text) component.findComponentById(ResourceTable.Id_tvRate);
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        rb1.setScore(SCORE);
        rb2.setScore(SCORE);
        rb3.setScore(SCORE);
        rb4.setScore(SCORE);
        rb5.setScore(SCORE);
        rb6.setScore(SCORE);

        tvRate.setText("rate:" + ratingBar3.getScore());
        ratingBar3.setRatingChangedListener(new Rating.RatingChangedListener() {
            @Override
            public void onProgressChanged(Rating rating, int index, boolean isB) {
                tvRate.setText("rate:" + rating.getScore() + "-- fromUser: " + isB);
            }

            @Override
            public void onStartTrackingTouch(Rating rating) {
            }

            @Override
            public void onStopTrackingTouch(Rating rating) {
            }
        });
    }
}